package com.boang.ref;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/*
  @author: 13813
  @date: 2024/12/12 9:24
*/
public class RefDemo02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //创建 class（字节码文件）文件对象
        //Class 描述 字节码文件（.class）
        Class<?> aClass = Class.forName("com.boang.ref.Student");

        //获取所有构造方法
        Constructor<?>[] constructors = aClass.getConstructors();

        List<Constructor<?>> list = Arrays.asList(constructors);

//        list.forEach(System.out::println);

        //获取指定构造方法
        Constructor<?> constructor = aClass.getConstructor(String.class, int.class);
        //通过反射创建对象
        Student student = (Student) constructor.newInstance("张三", 20);
//        System.out.println(student);

        //获取 Student类中所有方法
        Method[] methods = aClass.getMethods();
//        Arrays.asList(methods).forEach(System.out::println);

        //获取 Student类中所有成员变量
        Field[] fields = aClass.getDeclaredFields();
//        Arrays.asList(fields).forEach(System.out::println);

        //获取 Student类中所有注解
        Annotation[] annotations = aClass.getAnnotations();
        Arrays.asList(annotations).forEach(System.out::println);
    }
}
